from django.db import models
from Doctor_django.utils.models import BaseModel
from doctors.models import DoctorModel, AnswerModel
from users.models import UserModel


class CouponModel(BaseModel):
    DISCOUNT_CHOICES = {
        (1, '减免'),
        (2, '折扣'),
    }
    COUPON_TYPE_CHOICES = {
        (0, '通用类型'),
        (1, '指定医生'),
    }
    GET_TYPE_CHOICES = {
        (0, '系统赠送'),
        (1, '用户主动领取'),
    }
    discount = models.SmallIntegerField(choices=DISCOUNT_CHOICES, default=1, verbose_name='优惠方式')
    coupon_type = models.SmallIntegerField(choices=COUPON_TYPE_CHOICES, default=0, verbose_name='优惠劵类型')
    get_type = models.SmallIntegerField(choices=GET_TYPE_CHOICES, default=0, verbose_name='优惠劵领取方式')
    total = models.IntegerField(blank=True, null=True, default=100, verbose_name='发放数量')
    has_total = models.IntegerField(blank=True, null=True, default=100, verbose_name='剩余数量')
    start_time = models.DateTimeField(verbose_name='使用时间')
    end_time = models.DateTimeField(verbose_name='过期时间')
    condition = models.IntegerField(blank=True, default=0, null=True, verbose_name='满足优惠劵使用的价格条件')
    per_limit = models.SmallIntegerField(default=1, verbose_name='每人限制领取的数量')
    sale = models.CharField(max_length=100, verbose_name='优惠公式', help_text='*表示打折，示例：*0.8表示打八折<br/>-表示满减，示例：-100表示总价上减去100')

    class Meta:
        db_table = 'dc_coupon'
        verbose_name_plural = '优惠劵'


# 优惠劵与医生
class CouponDoctorModel(models.Model):
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_doctor', db_constraint=False, verbose_name='优惠劵')
    doctor = models.ForeignKey(to=DoctorModel, on_delete=models.CASCADE, related_name='to_coupon', db_constraint=False, verbose_name='医生')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        db_table = 'dc_coupon_doctor'
        verbose_name_plural = '优惠劵与医生'


# 优惠劵管理类
class CouponLogModel(BaseModel):
    USE_STATUS_CHOICES = {
        (0, '未使用'),
        (1, '已使用'),
        (2, '已过期'),
    }
    # 赠送给用户，是否使用  使用的时间，针对哪个订单使用
    # 与订单关联，等订单完成，再回来修改
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, related_name='to_coupon', db_constraint=False, verbose_name='用户')
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_user', db_constraint=False, verbose_name='优惠劵')
    use_time = models.DateTimeField(null=True, blank=True, verbose_name='使用时间')
    use_status = models.SmallIntegerField(choices=USE_STATUS_CHOICES, default=0, verbose_name='使用状态')
    order = models.ForeignKey(to='OrdersModel', on_delete=models.DO_NOTHING, null=True, blank=True, related_name='to_coupon', db_constraint=False, verbose_name='订单')

    class Meta:
        db_table = 'dc_coupon_log'
        verbose_name_plural = '优惠劵发放与日志管理类'

    def __str__(self):
        return f"{self.coupon.name}发放和使用日志"


# 写完修改  ： 修改优惠劵模型类
class OrdersModel(BaseModel):
    ORDER_STATUS_CHOICES = {
        (0, '未支付'),
        (1, '已支付'),
        (2, '已取消'),
        (3, '已超时'),
    }
    PAY_TYPE_CHOICES = {
        (0, '支付宝'),
        (1, '微信'),
    }
    total_price = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='订单总价')
    real_price = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='实付金额')
    order_number = models.CharField(max_length=64, verbose_name='订单号')
    order_status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=0, verbose_name='订单状态')
    pay_type = models.SmallIntegerField(choices=PAY_TYPE_CHOICES, default=0, verbose_name='支付方式')
    order_desc = models.TextField(null=True, blank=True, max_length=300, verbose_name='订单描述')
    pay_time = models.DateTimeField(null=True, blank=True, verbose_name='支付时间')
    user = models.ForeignKey(to=UserModel, related_name='user_orders', on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='用户')

    # 优惠劵
    def coupon(self):
        # 外键关联查询优惠劵信息
        coupon_related = self.to_coupon.first()
        # 拼接优惠劵信息
        if coupon_related:
            return {
                "id": coupon_related.coupon.id,
                "name": coupon_related.coupon.name,
                "sale": coupon_related.coupon.sale,
                "discount": coupon_related.coupon.discount,
                "condition": coupon_related.coupon.condition
            }
        return {}

    class Meta:
        db_table = 'dc_order'
        verbose_name_plural = '订单记录表'

    def __str__(self):
        return "%s 总价：%s，实付金额：%s" % (self.name, self.total_price, self.real_price)
    # 优惠劵


# 订单详情表
class OrdersDetailModel(BaseModel):
    order = models.ForeignKey(to=OrdersModel, related_name='order_detail', on_delete=models.CASCADE, db_constraint=False, verbose_name='订单')
    answer = models.ForeignKey(to=AnswerModel, related_name='order_detail', on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='问题')
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='医生原价')
    real_price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='优惠价格')
    discount_name = models.CharField(max_length=100, default="", verbose_name='优惠类型')

    class Meta:
        db_table = 'dc_order_answer'
        verbose_name_plural = '订单详情'

    def __str__(self):
        return self.answer.user_content
